package com.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.business.InfoBean;

/**
 * 
 * @author bob yang
 * @date 2018年3月27日 TODO xml处理与生成
 */
public class XmlUtil
{
	private static Logger logger = Logger.getLogger(XmlUtil.class);
	/**
	 * TODO 创建一个空的xml文档
	 * 
	 * @param fileName, projectPath
	 * @return true if the xmlFile create success/false if create failure
	 * @throws Exception
	 */
	public static void createXMLFile(String fileName, String projectPath) throws Exception
	{
		Document document = DocumentHelper.createDocument();
		String xmlFilePath = projectPath + fileName + ".xml";
		try
		{
			xmlFilePath = URLDecoder.decode(xmlFilePath, "utf-8");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		// Format xml格式，采用默认格式
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter xmlWriter = new XMLWriter(new FileWriter(xmlFilePath), format);
		xmlWriter.write(document);
		xmlWriter.close();
	}

	/**
	 *@ TODO 解析xml文件入口方法
	 *
	 * @param xmlFilePath
	 * @return Document
	 * @throws DocumentException
	 */
	public static Document readXMLFile(String xmlFilePath) throws Exception
	{
		SAXReader reader = new SAXReader();
		//reader.setEncoding("UTF-8");
		InputStream file = new FileInputStream(xmlFilePath); 
		InputStreamReader ir = new InputStreamReader(file, "UTF-8");
		Document document = reader.read(ir);
		
		return document;
	}

	/**
	 * @TODO 比较网页信息是否更新，有更新返回true，无则返回false
	 * 
	 * @param newList,projectPath
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean isUpdate(ArrayList<InfoBean> newList, String projectPath) throws Exception
	{
		try
		{
			Document document = readXMLFile(projectPath + "TJYZinfo.xml");
			Element rootElement = document.getRootElement();
			Element firstInfo = rootElement.element("detail");
			boolean isInfoUpdate = false;
			boolean isDateUpdate = false;
			isInfoUpdate = newList.get(0).getInfo().equals(firstInfo.getText());
			isDateUpdate = newList.get(0).getDate().equals(firstInfo.attributeValue("date"));
			if (isInfoUpdate && isDateUpdate)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		catch (Exception e)
		{
			logger.error("XmlUtil.java"+"-------->"+e.toString());
			throw new RuntimeException("比对前后信息时出现异常");
		}
	}

	/**
	 * @TODO 检查系统目录下xml文件是否存在，存在返回true，否则返回false
	 * 
	 * @param projectPath
	 * @return boolean
	 */
	public static boolean isXmlExist(String projectPath)
	{
		File file = new File(projectPath+"TJYZinfo.xml");
		if(file.exists())
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
